package com.vehicle.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Role;
import com.vehicle.mapper.RoleMapper;
import com.vehicle.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-28
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleMapper roleMapper;

    @GetMapping("/allList/{page}/{size}")
    public ResultVO getAllRoleList(@PathVariable("page") String page,@PathVariable("size") String size){
        Page<Role> roleInfo = new Page<>(Integer.parseInt(page),Integer.parseInt(size));
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        Page<Role> list = roleMapper.selectPage(roleInfo,queryWrapper);
        return ResultVO.succ(list);
    }

    //模糊查询
    @GetMapping("/likeList/{searchValue}")
    public ResultVO getLikeList(@PathVariable("searchValue") String searchValue){
        QueryWrapper<Role> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("role_id",searchValue);
        List<Role> list1 = roleMapper.selectList(queryWrapper1);
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.like("role_name",searchValue);
        List<Role> list2 = roleMapper.selectList(queryWrapper2);
        for (Role r:list2) {
            if(!list1.contains(r)){
                list1.add(r);
            }
        }
        return ResultVO.succ(list1);
    }

    //修改角色名称
    @PutMapping("/update/{roleId}/{roleName}")
    public ResultVO updatelRole(@PathVariable("roleId") Integer roleId,@PathVariable("roleName") String roleName){
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id",roleId);
        updateWrapper.set("role_name",roleName);
        int b = roleMapper.update(null,updateWrapper);
        if(b==1){
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",roleId);
            Role role = roleMapper.selectOne(queryWrapper);
            return ResultVO.succ(role);
        }
        return ResultVO.fail(null);
    }

    //删除角色
    @DeleteMapping("/delete/{roleId}")
    public ResultVO deleteRole(@PathVariable("roleId") Integer roleId){
       int b = roleMapper.deleteById(roleId);
       if(b==1){
           return ResultVO.succ(true);
       }
        return ResultVO.fail(null);
    }

    //添加角色
    @PostMapping("/add/{roleName}")
    public ResultVO addRole(@PathVariable("roleName") String roleName){
        Role role = new Role();
        role.setRoleName(roleName);
        int t = roleMapper.insert(role);
        if(t==1){
            return ResultVO.succ(role);
        }
        return ResultVO.fail(null);
    }

    @GetMapping("/allName")
    public ResultVO getAllRoleName(){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> roles = roleMapper.selectList(queryWrapper);
        List<String> names = new ArrayList<>();
        for (Role r:roles) {
            names.add(r.getRoleName());
        }
        return ResultVO.succ(names);
    }

    //根据角色id获取角色名称
    @GetMapping("/getRoleName/{roleId}")
    public String getRoleNameById(@PathVariable("roleId") Integer roleId){
        Role role = roleMapper.selectById(roleId);
        return role.getRoleName();
    }

}

